<template>
	<template v-if='box'>
		<Box title='附件信息' :titleStyleMode='2' class='mt5' :loading='loading' :titleConfig='affixControlsConfig'>
			<Table :config='tableConfig' ref='tableRef' :bordered='false'/>
		</Box>
	</template>
	<template v-else>
		<Table :config='tableConfig' ref='tableRef' :bordered='false'/>
	</template>
	<UploadBusinessModal ref='uploadBusinessModalRef' @change='onUploadChange' />
</template>

<script setup>
	/* --------------------------- Component --------------------------- */
	const Box = $.lazy(()=>import('#tp/box/box.vue'))
	const Table = $.lazy(()=>import('@antd/table.vue'))
	const UploadBusinessModal 	= $.lazy(()=>import('#pub/upload-business.vue'))
	
	const { control, add } = defineProps({
		loading: Boolean,
		box: Boolean,
		control: Boolean,
		add: Boolean
	})
	/* --------------------------- Table  --------------------------- */
	ref: tableRef
	let cols = [
		{ title: '文件名称', 	dataIndex: 'fileName', 			width:150,  customRender:({ text, record }) => $fn.table.link(text, record.url) },
		{ title: '业务类型', 	dataIndex: 'businessTypeName', 	...$fn.table.name },
		{ title: '更新时间', 	dataIndex: 'createTime', 		width:100 },
		{ title: '版本', 		dataIndex: 'version', 			...$fn.table.time },
		{ title: '类型', 		dataIndex: 'extension', 		width:100 },
		{ title: '大小', 		dataIndex: 'size', 				...$fn.table.time },
		{ title: '更新人', 		dataIndex: 'createByCN', 		...$fn.table.time }
	]
	if(control){
		cols.push({ ...$fn.table.controlsSelect })
	}
	
	ref: tableConfig = {
		ref 		: () => tableRef,
		auto		: true,
		cols,
		controls 	: row => {
			const { filePath, url, fileId, fileName } = row
			return [
				{ label:'在线浏览', link: url },
				{ label:'下载', mode:'download', param:{ filePath }, fileName },
				// { label:'更新', mode:'upload', way:2, param:{ fileId }, change:()=> getAccessory() },
				// { label:'删除' },
			]
		}
	}
	/* --------------------------- 添加附件  --------------------------- */
	ref: uploadBusinessModalRef
	let param = {}
	const affixControlsConfig = add ? {
		controls: [
			{ label:'添加附件', mode:'upload', click:()=> {
				console.log(param)
				uploadBusinessModalRef.open(param)
			} }
		]
	} : []
	
	const emit = defineEmits([ 'change' ])
	
	let uploadData = [ ]
	const onUploadChange = data => {
		if($fn.hasArray(data)){
			uploadData = [ ...uploadData, ...data ]
		}else{
			uploadData.push(data)
		}
		tableRef.setData( [...uploadData] )
		emit('change', uploadData)
	}
	/* ---------------------------   --------------------------- */
	
	defineExpose({
		setData: data => {
			uploadData = data
			tableRef?.setData?.(data)
		},
		setParam: value => param = value
	})
</script>
